package customhandler;


import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.util.ArrayList;
import java.util.List;
import baseinterface.BaseInterface;


/**
 * 全局消息处理器
 * 
 */
public class MyHandler extends Handler implements BaseInterface {

	public static final int MESSAGE_HANDLED = -1;

//	private MyApplication mContext;
	private static List<Handler> handlerList = new ArrayList<Handler>();

	public MyHandler() {
		super(Looper.getMainLooper());
	}

	public synchronized void registeHandler(Handler handler) {
		if (!handlerList.contains(handler)) {
			handlerList.add(handler);
		}
	}

	public synchronized void unregistHandler(Handler handler) {
		if (handlerList.contains(handler)) {
			handlerList.remove(handler);
		}
	}

	@Override
	public void handleMessage(Message msg) {
		try {
			synchronized (this) {

				// 子处理器执行,最后注册的先接
				for (int index = handlerList.size() - 1; index >= 0; index--){
						handlerList.get(index).handleMessage(msg);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



}